<template>
  <view>

    <!-- 组件方式实现 -->
    <QuestionnaireList ref="questionnaire" :list='questions' @answer="answerFn"/>

    <view class="blank"></view>
    <view class="btn" @click="submitQuestionnaire('null')">提交问卷</view>
    <view class="blank"></view>
    <view class="blank"></view>

  </view>
</template>

<script>
import request from "@/utils/request.js"
import QuestionnaireList from "@/components/questionnaire/QuestionnaireList.vue"

export default {
  data() {
    return {
      questions: [],
      answerList: [],
      questionAnswerList: [],
      age: '',
      gender: '',
      loading: false
    }
  },
  onLoad(options) {
    this.age = options.age
    this.gender = options.gender
    this.init()
  },
  components: {
    QuestionnaireList,
  },
  methods: {
    answerFn(item) {
      //方案2收集答案
      let qList = this.answerList
      let index = qList.findIndex(obj => obj.id === item.id)
      index === -1 ? qList.push(item) : qList[index] = item
      this.answerList = qList
      console.log("this.answerList", this.answerList)
    },
    async init() {
      let res = await request.post({
        service: "smarthos.zuobiao.cancer.digestive.tract.questionnaire.list"
      })
      console.log("res", res)
      this.questions = res.obj.questions
    },

    /**
     * 提交问卷
     * 2024.08.12
     * @returns {Promise<void>}
     */
    async submitQuestionnaire() {
      if (this.loading) return
      this.processQues('null') && await this.submitFn()
    },

    /**
     * 处理数据
     * 2024.07.26
     */
    processQues(dataList) {
      let list = []
      if (dataList === 'null') {
        list = this.questions
      } else {
        list = dataList
      }

      let questionAnswerList = this.answerList.map(item => {
        return {
          questionId: item.id,
          optionValue: item.fValue
        }
      })
      this.questionAnswerList = questionAnswerList

      let verifyPass = true
      for (let item of list) {
        if (item.subQuestions && item.subQuestions.length > 0) {
          this.processQues(item.subQuestions)
        } else {
          let quesItem = questionAnswerList.find(ele => ele.questionId === item.id)
          console.log("quesItem", quesItem);
          if (!quesItem) {
            uni.showToast({
              title: `${item.questionNumber}、${item.questionName}不能为空`,
              icon: "none"
            })
            verifyPass = false
            break;
          }

          if (item.type === 'text_int') {
            if (!/^\d+$/.test(quesItem.optionValue)) {
              uni.showToast({
                title: `${item.questionName}只能是数字`,
                icon: "none"
              })
              verifyPass = false
              break;
            }
          }

          if (item.identifyKey === 'phone') {
            if (!/^1[3-9]\d{9}$/.test(quesItem.optionValue)) {
              uni.showToast({
                title: `手机号码格式错误`,
                icon: "none"
              })
              verifyPass = false
              break;
            }
          }
        }
      }
      return !!verifyPass
    },
    /**
     * 提交
     */
    async submitFn() {
      this.loading = true
      let payload={
        service: "smarthos.zuobiao.cancer.digestive.tract.questionnaire.answer.save",
        patSex: this.gender,
        age: this.age,
        questionAnswerList: this.questionAnswerList,
      }
      if(this.$store.state.FPorgId){
        payload.hosId = this.$store.state.FPorgId
      }
      let res = await request.post(payload)
      this.loading = false
	  console.log("fdsafsfds===>",res)
	  uni.showToast({
	    title: res.msg,
	    icon: "none"
	  })
      if (res.code === '0') {
        uni.setStorageSync("resData", res.obj)
        setTimeout(() => {
          uni.navigateTo({
            url: '/Questionnaire/questionnaire-result'
          })
        }, 1500)
      }
      console.log("提交成功", res)
    },
  }
}
</script>

<style>

</style>